<template>
    <el-container class="lesson">
        <el-page-header @back="goBack" :content="`${title} - 课程管理`">
        </el-page-header>
        <div class="lesson-header">
            <el-button size="small" type="primary" @click="openDialog('add')">添加课程</el-button>
        </div>

        <el-main class="lesson__content">
            <div ref="container" class="table-container">
                <el-table ref="table" :data="datas" style="width: 100%" align="center" header-align="center">
                    <el-table-column label="课程名称" prop="title" center />

                    <el-table-column label="课程封面" center width="160px">
                        <template slot-scope="scope">
                            <el-image :src="scope.row.coverImage" />
                        </template>
                    </el-table-column>

                    <el-table-column label="考试题目总数" prop="questionCount" center />

                    <el-table-column label="考试通过正确率" center>
                        <template slot-scope="scope">
                            {{ scope.row.passRate }}%
                        </template>
                    </el-table-column>
                    
                    <el-table-column label="课程简介" prop="desc">
                    </el-table-column>

                    <el-table-column label="内容" center width="300px">
                        <template slot-scope="scope">
                            <el-button size="small" type="primary" @click="toVideo(scope.row)">视频</el-button>
                            <el-button size="small" type="success" @click="toQuestionBack(scope.row)">题库</el-button>
                            <el-button size="small" @click="toPerson(scope.row)">会员名单</el-button>
                        </template>

                    </el-table-column>

                    <el-table-column label="操作" center width="200px">
                        <template slot-scope="scope">
                            <el-button type="primary" size="small" @click="openDialog('edit', scope.row)">编辑</el-button>
                            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
        </el-main>
        <dialog-edit :visible.sync="dialogVisible" v-if="dialogVisible" @refresh="refresh" ref="edit"></dialog-edit>


    </el-container>
</template>

<script>
import dialogEdit from "./components/edit.vue";

export default {
    name: "lessonList",
    components: {
        dialogEdit,
    },
    data() {
        return {
            dialogVisible: false,
            datas: [],

        };
    },

    computed: {

        title() {
            const { title } = this.$route.query;
            return title;
        },
    },

    created() {
        this.refresh();
    },
    methods: {

        async handleDatas() {
            const loading = this.$loading();
            try {
                const { topicId } = this.$route.query;
                const { data } = await this.$fetch.get("/api/topic-lesson/list", {
                    topicId,
                });
                this.datas = data
            } catch (error) {
                console.log(error);
            }
            loading.close();
        },
        refresh() {
            this.handleDatas();
        },

        openDialog(type, row) {
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.$refs.edit.init(type, row);
            })
        },

        handleDelete(row) {
            this.$confirm("确定要删除此课程？", "", {
                distinguishCancelAndClose: true,
                confirmButtonText: "确认",
                cancelButtonText: "取消",
            }).then(async () => {
                const { code } = await this.$fetch.post(
                    "/api/topic-lesson/delete",
                    row
                );
                if (code === 0) {
                    this.$message.success("删除成功");
                }
                this.refresh();
            });
        },

        toVideo(item) {
            this.$router.push({
                path: "/topic/lesson-video",
                query: {
                    lessonId: item.id,
                    topicId: item.topicId,
                    title: item.title,
                },
            });
        },

        toQuestionBack(item) {
            this.$router.push({
                path: "/topic/lesson-question-bank",
                query: {
                    lessonId: item.id,
                    topicId: item.topicId,
                    title: item.title,
                },
            });
        },

        toPerson(item) {
            this.$router.push({
                path: "/topic/lesson-person",
                query: {
                    lessonId: item.id,
                    topicId: item.topicId,
                    title: item.title,
                },
            });
        },



        goBack() {
            this.$router.go(-1);
        }

    },
};
</script>

<style scoped lang="scss">
.lesson {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 50px);

    .title {
        font-size: 26px;
        width: 100%;
        line-height: 42px;
        justify-content: center;
        align-items: center;
        display: flex;
    }

    .lesson-header {
        height: 40px;
        display: flex;
        align-items: center;
        margin-top: 30px;
    }

    .lesson__content {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;
        padding-bottom: 0;

        .table-container {
            flex: 1;
            overflow: hidden;
            overflow-y: auto;
        }
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;

        .el-pagination {
            padding: 20px 0;
        }
    }
}
</style>
